<template>
  <div>
    <nav-bar></nav-bar>
    <van-tabs v-model="active" @click="choose">
      <van-tab :title="item.name" v-for="item in navData" :key="item.id">
        <div class="info">
          <p class="name">{{ item.name }}</p>
          <p class="desc">{{ item.front_name }}</p>
        </div>
        <div class="goodList" v-if="num != 0">
          <div
            class="item"
            v-for="item in goodsList"
            :key="item.id"
            @click="goGoods(item.id)"
          >
            <img :src="item.list_pic_url" alt="" />
            <p class="name">{{ item.name }}</p>
            <p class="price">￥{{ item.retail_price }}</p>
          </div>
        </div>
        <div class="banner-info" v-if="num == '0'">数据库暂无数据</div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Navbar from "../../../components/Navbar";
import { categoryNav } from "@/api/home/cate/index.js";
import { goodsList } from "@/api/home/current/goodlist.js";

export default {
  props: {},
  data() {
    return {
      active: 0,
      navData: [],
      goodsList: [],
      id: this.$route.query.ids,
      num: "",
    };
  },
  computed: {},
  created() {
    const id = this.$route.query.ids;
    //  console.log(id);
    categoryNav({
      id: this.id,
    }).then((res) => {
      // console.log(res);
      this.navData = res.navData;
      for (let i = 0; i < this.navData.length; i++) {
        if (id == this.navData[i].id) {
          this.active = i;
        }
      }
    });
    //   goodsList({
    //       categoryId:this.id
    //   }).then(res=>{
    //       console.log(res);
    //       this.goodsList=res.data
    //   })
  },
  mounted() {
    this.getGoodsList(this.id);
  },
  watch: {},
  methods: {
    getGoodsList(id) {
      goodsList({
        categoryId: id,
      }).then((res) => {
        //   console.log(res);
        this.goodsList = res.data;
        this.num = this.goodsList.length;
        // console.log(this.goodsList)
      });
    },
    choose(index) {
      //   console.log(this.navData[index].id);
      let id = this.navData[index].id;
      this.getGoodsList(id);
    },
    goGoods(id) {
      this.$router.push({ path: "/home/goods", query: { ids: id } });
    },
  },
  components: {
    "nav-bar": Navbar,
  },
};
</script>

<style lang="scss" scoped>
.info {
  text-align: center;
  background: #fff;
  padding: 15px;
  margin-top: 10px;
  margin-bottom: 2;
  .name {
    margin-bottom: 9px;
    font-size: 15px;
    color: #333;
  }
  .desc {
    display: block;
    height: 12px;
    font-size: 12px;
    color: #999;
  }
}
.goodList {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 186px;
    margin-bottom: 2px;
    text-align: center;
    background: #fff;
    padding: 7.5px 0;
    img {
      display: block;
      width: 151px;
      height: 151px;
      margin: 0 auto;
    }
    .name {
      margin: 7px 0 11px 0;
      text-align: center;
      padding: 0 10px;
      font-size: 12px;
    }
    .price {
      text-align: center;
      font-size: 15px;
      color: #b4282d;
    }
  }
}
.banner-info {
  text-align: center;
  color: #999;
  font-size: 12px;
  margin-top: 2.66667rem;
}
</style>
